layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var resultData;

    var selectType;
    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    if (!Feng.isUndefined(Feng.getUrlParam("materielId"))) {
        var ajax = new $ax(Feng.ctxPath + "/api/materiel/detail?materielId=" + Feng.getUrlParam("materielId"));
        var result = ajax.start();
        resultData = result.data;
        form.val('materielForm', result.data);
        selectType = resultData.type + '';
    }


    //初始化数据

    loadResultData();
    function loadResultData() {
        loadTypeList();
    }
    /**
     * 加载类型
     */

    function loadTypeList() {

        $("#typeSelect").html('');
        var ajax = new $ax(Feng.ctxPath + "/sysConfig/list?dictTypeId=11", function (data) {
            for (var i = 0; i < data.data.length; i++) {
                var name = data.data[i].name;
                var value = data.data[i].value;
                var selected = '';
                if (value === selectType) {
                    selected = 'selected';
                }
                if (i === 0 && Feng.isUndefined(selectType) && value !== selectType) {
                    selectType = value;
                    selected = 'selected';
                }
                $("#typeSelect").append('<option value="' + value + '"  ' + selected + '>' + name + '</option>');
            }
            form.render();

        }, function (data) {
        });
        ajax.start();
    }

    form.on('select(typeSelect)', function (data) {
        selectType = data.value;
    });

    //表单提交事件
    var url = "/api/materiel/addItem";
    if (!Feng.isUndefined(Feng.getUrlParam("materielId"))) {
        url = "/api/materiel/editItem";
    }
    form.on('submit(btnSubmit)', function (data) {
        data.field.type = selectType;
        var ajax = new $ax(Feng.ctxPath + url, function (data) {
            Feng.success("操作成功！");
            window.location.href = Feng.ctxPath + '/api/materiel'
        }, function (data) {
            Feng.error("操作失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/api/plan'
    });

});